<?php
class ShopCartClass {

	function ShopCartClass () {
		global $setupdata, $mysqlclass, $treeclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->treeclass=$treeclass;
	}
	
	function ClearCart($cart_id){
		if (strlen($cart_id) > 0){
			$this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."shop_cart` where user_id = '".$cart_id."'", __LINE__, __FILE__);
		}
	}
	
	function AddCart($cart_id, $product, $count){
		if ((is_array($product)) and (is_array($count))){
			if (count($product) > 0){
				for ($i=0; $i<count($product); $i++) {
					if (strlen($product[$i]) > 0){					
						if (strlen($count[$i]) == 0) $count[$i] = 0;
						$this->AddCartOnce($cart_id, $product[$i], $count[$i]);
					}
				}
			}
		} else {
			if (strlen($count) == 0) $count = 0;
			if ((strlen($product) > 0) and (strlen($cart_id) > 0)){
				 $this->AddCartOnce($cart_id, $product, $count);
			}
		}
		$this->DelTimeOut();
	}
	
	function AddCartOnce($cart_id, $product, $count){
		if (strlen($count) == 0) $count = 0;
		if ((strlen($product) > 0) and (strlen($cart_id) > 0)){
			$dat = date("YmdHis");
			$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart` where user_id = '".$cart_id."' and product_id = ".$product, __LINE__, __FILE__);
			$row = mysql_num_rows($sql);
			if ($row > 0) {
				$sql = "UPDATE `".$this->setupdata['table_prefix']."shop_cart` SET product_count='".$count."', add_time='".$dat."' WHERE user_id = '".$cart_id."' and product_id = ".$product;
				$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
			} else {
				$sql = "INSERT INTO `".$this->setupdata['table_prefix']."shop_cart` (`user_id`,`product_id`,`product_count`,`add_time`) VALUES ('$cart_id', '$product', $count, '$dat')";
				$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
			}
		}
	}
	
	function GetCart($cart_id){
		$this->DelTimeOut();
		$rows = array();
		$rows['total_count'] = 0;
		if (strlen($cart_id) > 0){
			$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart` where user_id = '".$cart_id."'", __LINE__, __FILE__);
			$num = mysql_num_rows($sql);
			if ($num > 0) while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$arr = array();
				$arr = $this->treeclass->get_item_data($row['product_id']);
				foreach ($arr as $val) $row['data'][$val['name']] = $val['value'];
				$arr = $this->treeclass->get_item_info($row['product_id']);
				$row['name'] = $arr['name'];
				$rows['total_count'] = $rows['total_count'] + $row['product_count'];
				$rows[$row['product_id']] = $row;
			}
		}
		return $rows;
	}
	
	function RemoveCart($cart_id, $product){
		if ((strlen($product) > 0) and (strlen($cart_id) > 0)){
			$sql = $this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."shop_cart` where user_id = '".$cart_id."' and product_id = ".$product, __LINE__, __FILE__);
		}
	}
	
	function DelTimeOut(){
		$tm = date("YmdHis");
	  	$timeout = 86400;
		$sql = $this->mysqlclass->mysql_my_query("delete from `".$this->setupdata['table_prefix']."shop_cart` where $tm-add_time>$timeout", __LINE__, __FILE__);
	}
	
	function GetLastOrder(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(order_id) from `".$this->setupdata['table_prefix']."shop_cart_orders`", __LINE__, __FILE__);
		$row = mysql_fetch_array($sql, MYSQL_ASSOC);
		return $row['MAX(order_id)'];
	}
	
	function OrderCart($user_id, $cart_id, $status="", $cost_value="product01_cost"){
		if (strlen($cart_id) > 0){
			if ($status == 'del') $sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."shop_cart_orders` WHERE `client_id` = '".$user_id."'", __LINE__, __FILE__);		
			$card = $this->GetCart($cart_id);
			$order_id = $this->GetLastOrder() + 1;
			foreach ($card as $value) {
				$product_id = $value['product_id'];
				$count = $value['product_count'];
				$data = $this->treeclass->get_item_content($product_id);
				if  (isset($data[$cost_value])) $cost = $data[$cost_value]['value'];
				else $cost = 0;
				$sql = "INSERT INTO `".$this->setupdata['table_prefix']."shop_cart_orders` (`dattime`,`order_id`,`client_id`,`product_id`,`cost`,`count`) VALUES (NOW(), $order_id, '$user_id', $product_id, '$cost', $count)";
				$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
			}
			$this->ClearCart($cart_id);
		}
	}
}
?>
